<?php
session_start();

include 'config.php'; 
?>
<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Markdown博客</title>
    <link href="/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.7.0/styles/github-dark.min.css">
    <style>
        .article-card {
            transition: transform 0.3s ease, box-shadow 0.3s ease;
        }

        .article-card:hover {
            transform: translateY(-5px);
            box-shadow: 0 10px 20px rgba(0, 0, 0, 0.1);
        }

        .article-excerpt {
            display: -webkit-box;
            -webkit-line-clamp: 3;
            -webkit-box-orient: vertical;
            overflow: hidden;
        }

        .footer-section {
            margin-top: 30px;
            padding-top: 20px;
            border-top: 1px solid #eee;
        }

        .qrcode-container {
            text-align: center;
            margin: 20px 0;
        }

        .qrcode-container img {
            max-width: 200px;
            height: auto;
        }
    </style>
</head>

<body>
<nav class="navbar navbar-expand-lg navbar-dark bg-dark mb-4">
    <div class="container">
        <a class="navbar-brand" href="index.php">Markdown博客</a>
        <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav">
            <span class="navbar-toggler-icon"></span>
        </button>
        <div class="collapse navbar-collapse" id="navbarNav">
            <ul class="navbar-nav me-auto">
                <li class="nav-item">
                    <a class="nav-link active" href="index.php">首页</a>
                </li>
                <!-- 只有登录后才显示发布文章和设置选项 -->
                <?php if (isset($_SESSION['user_id'])): ?>
                <li class="nav-item">
                    <a class="nav-link" href="create.php">发布文章</a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="settings.php">设置</a>
                </li>
                <?php endif; ?>
            </ul>
            
            <!-- 根据登录状态显示不同内容 -->
            <?php if (isset($_SESSION['user_id'])): ?>
                <div class="dropdown">
                    <button class="btn btn-outline-light dropdown-toggle" type="button" data-bs-toggle="dropdown">
                        <?php echo htmlspecialchars($_SESSION['username']); ?>
                    </button>
                    <ul class="dropdown-menu dropdown-menu-end">
                        <li><a class="dropdown-item" href="logout.php">退出登录</a></li>
                    </ul>
                </div>
            <?php else: ?>
                <!-- 未登录：显示登录按钮 -->
                <a href="login.php" class="btn btn-outline-light">登录</a>
            <?php endif; ?>
        </div>
    </div>
</nav>

    <div class="container">
        <h1 class="mb-4">最新文章</h1>

        <div class="row">
            <?php
            // 获取所有文章
            $stmt = $pdo->query("SELECT * FROM aiwz_articles ORDER BY created_at DESC");
            $articles = $stmt->fetchAll(PDO::FETCH_ASSOC);

            if (count($articles) > 0) {
                foreach ($articles as $article) {
                    // 解析Markdown获取摘要（纯文本）                  
                    $parsedown = new Parsedown();
                    $htmlContent = $parsedown->text($article['markdown_content']);
                    //$htmlContent = Markdown::defaultTransform($article['markdown_content']);
                    $textContent = strip_tags($htmlContent);
                    ?>
                    <div class="col-md-6 col-lg-4 mb-4">
                        <div class="card article-card h-100">
                            <div class="card-body">
                                <h5 class="card-title"><?php echo htmlspecialchars($article['title']); ?></h5>
                                <h6 class="card-subtitle mb-2 text-muted">
                                    <?php echo htmlspecialchars($article['category']); ?> •
                                    <?php echo date('Y-m-d', strtotime($article['created_at'])); ?>
                                </h6>
                                <p class="card-text article-excerpt"><?php echo $textContent; ?></p>
                                <div class="tags mb-3">
                                    <?php
                                    $tags = explode(',', $article['tags']);
                                    foreach ($tags as $tag) {
                                        echo '<span class="badge bg-secondary me-1">' . htmlspecialchars(trim($tag)) . '</span>';
                                    }
                                    ?>
                                </div>
                                <a href="article.php?id=<?php echo $article['id']; ?>" class="btn btn-primary">阅读全文</a>
                            </div>
                        </div>
                    </div>
                    <?php
                }
            } else {
                echo '<div class="col-12"><div class="alert alert-info">暂无文章，快去发布第一篇文章吧！</div></div>';
            }
            ?>
        </div>
    </div>

    <footer class="bg-dark text-white mt-5 py-4">
        <div class="container text-center">
            <p>&copy; <?php echo date('Y'); ?> Markdown博客 - 用技术记录生活</p>
        </div>
    </footer>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.7.0/highlight.min.js"></script>
    <script>hljs.highlightAll();</script>
</body>

</html>